package cn.edu.zzuli.web.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.zzuli.web.Constant;
import cn.edu.zzuli.web.bean.DocumentBean;
import cn.edu.zzuli.web.controller.model.UploadForm;

@Controller
public class DocumentController extends AbstractController {

	/**
	 * 文档列表页面
	 */
	@RequestMapping(value = "/document/list", method = RequestMethod.GET)
	public String documentList(HttpServletRequest request,
			HttpServletResponse response, String path) {

		Map<String, DocumentBean> map = new HashMap<String, DocumentBean>();
		String rootPath = null;
		if (path == null || path.trim().equals("")) {
			rootPath = Constant.UPLOAD_FILE_DIR;
		} else {
			rootPath = Constant.UPLOAD_FILE_DIR + "\\" + path;
		}
		File file = new File(rootPath);
		if (file.isDirectory()) {
			String[] filelist = file.list();
			for (int i = 0; i < filelist.length; i++) {
				File readfile = new File(rootPath + "\\" + filelist[i]);
				DocumentBean bean = new DocumentBean();
				if (!readfile.isDirectory()) {
					bean.setPath(rootPath);
					bean.setType("文件");
					map.put(readfile.getName(), bean);
				} else if (readfile.isDirectory()) {
					bean.setPath(filelist[i]);
					bean.setType("文件夹");
					map.put(readfile.getName(), bean);
				}
			}
		}

		request.setAttribute("directorys", map);
		return "/document/list";
	}

	@RequestMapping(value = "/document/download", method = RequestMethod.GET)
	public String documentDownload(HttpServletRequest request,
			HttpServletResponse response, String path, String filename)
			throws Exception {

		File file = new File(path + "\\" + filename);
		response.setContentType("application/x-msdownload");
		response.setContentLength((int) file.length());
		response.setHeader("Content-Disposition", "attachment;filename="
				+ new String(filename.getBytes("utf-8"), "utf-8"));

		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream buff = new BufferedInputStream(fis);

		byte[] b = new byte[1024];
		long k = 0;
		OutputStream myout = response.getOutputStream();

		while (k < file.length()) {
			int j = buff.read(b, 0, 1024);
			k += j;
			myout.write(b, 0, j);
		}
		myout.flush();
		return null;
	}

	/**
	 * 文档上传页面
	 */
	@RequestMapping(value = "/document/add", method = RequestMethod.GET)
	public String documentAdd(Map<Object, Object> model) {
		model.put("uploadForm", new UploadForm());
		return "/document/add";
	}

	@RequestMapping(value = "/document/upload", method = RequestMethod.POST)
	public String upload(@Valid
	UploadForm uploadForm, BindingResult result ,String path) {
		MultipartFile file = uploadForm.getFile();
		String kuozhan = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
		
		if (result.hasErrors() || !kuozhan.trim().equals("zip")) {
			return "/document/add";
		}
		try {
			byte[] bytes = file.getBytes();
			File save = new File(Constant.UPLOAD_FILE_DIR + "\\" + path + "\\" + file.getOriginalFilename());
			FileCopyUtils.copy(bytes, save);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "redirect:/document/list.html";
	}

}
